package org.springblade.tms.module.transportorder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springblade.tms.module.demandextrainfo.base.DemandExtraService;
import org.springblade.tms.module.demandextrainfo.entity.TmDemandExtraInfo;
import org.springblade.tms.module.demandextrainfo.vo.TmDemandExtraInfoVO;
import org.springblade.tms.module.exceptionrecord.dto.TMExceptionRecordDTO;
import org.springblade.tms.module.exceptiontype.base.ExceptionMoldService;
import org.springblade.tms.module.exceptiontype.entity.TMExceptionType;
import org.springblade.tms.module.exceptiontype.vo.TMExceptionTypeVO;
import org.springblade.tms.module.signedinfo.dto.TMSignedInfoDTO;
import org.springblade.tms.module.transportorder.base.TransportListService;
import org.springblade.tms.module.transportorder.base.WaybillService;
import org.springblade.tms.module.transportorder.dto.TMTransportOrderDTO;
import org.springblade.tms.module.transportorder.dto.WaybillStatusChangeDto;
import org.springblade.tms.module.transportorder.vo.TMTransportOrderVO;
import org.springblade.tms.module.transportorder.vo.TransportOrderVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.transportorder.controller
 * @className: WaybillController
 * @author: yr
 * @description: 小程序运单接口
 * @date: 2022/3/16 11:54
 */
@RestController
@RequestMapping("transportorder/Waybill")
@Api(value = "小程序运单-yr", tags = "小程序运单接口-yr")
public class WaybillController {
	@Resource
	private WaybillService waybillService;
	@Resource
	private TransportListService transportListService;
	@Resource
	private ExceptionMoldService exceptionMoldService;
	@Resource
	private DemandExtraService demandExtraService;

	/**
	 * @param transportOrderDTO:
	 * @return R<IPage < TMTransportOrderVO>>
	 * @author yr
	 * @description 运输单列表查询
	 * @date 2022/3/16 14:18
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "运输单列表", notes = "传入transportOrder")
	@PreAuth("hasAnyRole('admin','administrator','carrier','driver')")
	public R<IPage<TMTransportOrderVO>> list(TMTransportOrderDTO transportOrderDTO, Query query) {
		return waybillService.list(transportOrderDTO, query);
	}

	/**
	 * @param waybillStatusChangeDto:
	 * @return null
	 * @author yr
	 * @description 运单状态变更
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/waybillStatusChange")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "运单状态变更  操作枚举：operating", notes = "传入waybillStatusChangeDto")
	public R<Boolean> waybillStatusChange(@RequestBody WaybillStatusChangeDto waybillStatusChangeDto) {
		return transportListService.waybillStatusChange(waybillStatusChangeDto);
	}

	/**
	 * @param signedInfoDTO:
	 * @return null
	 * @author yr
	 * @description 签收
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/signForReceipt")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "签收", notes = "传入signedInfoDTO")
	public R<Boolean> signForReceipt(@RequestBody TMSignedInfoDTO signedInfoDTO) {
		return transportListService.signForReceipt(signedInfoDTO);
	}

	/**
	 * @param exceptionRecordDTO:
	 * @return R<Boolean>
	 * @author yr
	 * @description 异常汇报
	 * @date 2022/3/21 17:47
	 */
	@PostMapping("/exceptionReport")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "异常汇报", notes = "传入exceptionRecordDTO")
	public R<Boolean> exceptionReport(@RequestBody TMExceptionRecordDTO exceptionRecordDTO) {
		return waybillService.exceptionReport(exceptionRecordDTO);
	}

	/**
	 * @param :
	 * @return R<TMExceptionTypeVO>
	 * @author yr
	 * @description 异常类型查询
	 * @date 2022/3/21 18:02
	 */
	@GetMapping("/exceptionType")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "异常类型查询")
	public R<IPage<TMExceptionTypeVO>> exceptionType() {
		return exceptionMoldService.list(new TMExceptionType(), new Query());
	}

	/**
	 * @param id:
	 * @return null
	 * @author yr
	 * @description 运输单详情
	 * @date 2022/3/17 10:45
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "运输单详情", notes = "传入demandOrderId")
	public R<TransportOrderVo> detail(Long id) {
		return transportListService.detail(id);
	}

	/**
	 * @param transportOrderDTO:
	 * @return null
	 * @author yr
	 * @description 签收
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "运单修改", notes = "传入transportOrderDTO")
	public R update(@RequestBody TMTransportOrderDTO transportOrderDTO) {
		return transportListService.update(transportOrderDTO);
	}

	/**
	 * 补全信息
	 */
	@PostMapping("fillTransport")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "运单补充信息补全信息", notes = "id不用传")
	public R<Long> fillTransport(@RequestBody @Validated(TmDemandExtraInfo.FILL_INFO_Transport.class) TmDemandExtraInfoVO input) {
		return R.data(demandExtraService.fillTransportExtra(input));
	}

	/**
	 * 删除 需求单
	 */
	@PostMapping("/invalid")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "运输单作废", notes = "传入id")
	public R invalid(@RequestBody TMTransportOrderVO tmTransportOrderVO) {
		return waybillService.invalid(tmTransportOrderVO.getId(), tmTransportOrderVO.getReasonForVoiding());
	}
}
